package org.example.struct.array3;

/**
 * @author liyishan
 * @date 2024/3/19 10:37
 * @apiNote
 */

public class Test01793MaximumScore {

    public static void main(String[] args) {
        int[] nums = new int[]{1,4,3,7,4,5};
        int k = 3;
        int res = maximumScore(nums, k);
        System.out.println(res);
    }

    public static int maximumScore(int[] nums, int k) {
        int n = nums.length;
        int left = k - 1, right = k + 1;
        int ans = 0;
        for (int i = nums[k]; ; i--) {
            while (left >= 0 && nums[left] >= i) {
                left--;
            }
            while (right < n && nums[right] >= i) {
                right++;
            }
            ans = Math.max(ans, (right - left - 1) * i);
            if (left == -1 && right == n) {
                break;
            }
        }
        return ans;
    }
}
